<template>
  <div>
    <el-input v-model="searchForm.username" style="width: 200px" placeholder="请输入姓名" clearable></el-input>
    <!-- <el-input v-model="searchForm.phone" style="width: 200px; margin-left: 5px" placeholder="请输入电话"></el-input> -->
    <el-button type="warning" style="margin-left: 10px" @click="findBySearch()">查询</el-button>
    <el-button type="primary" style="margin-left: 10px" @click="add()">新增</el-button>
    <el-table :data="tableData" style="width: 100%; margin: 15px 0px">
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <!-- <el-table-column prop="department" label="部门"></el-table-column> -->
      <!-- <el-table-column prop="phone" label="联系方式"></el-table-column> -->
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <!-- <el-table-column prop="img" label="头像"></el-table-column> -->
      <el-table-column prop="role" label="权限">
        <template #default="scope">
          <!-- <span style="margin-left: 10px" v-if="scope.row.role == '3'">普通用户</span> -->
          <span v-if="scope.row.status == '2'">普通用户</span>
          <span v-if="scope.row.status == '1'">管理员</span>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="status" label="状态"></el-table-column> -->
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="primary" @click="edit(scope.row)">修改</el-button>
          <el-popconfirm title="确定删除吗？" @confirm="del(scope.row)">
            <template #reference>
              <el-button type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin-top: 10px">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]" :page-size="currentSize" layout="total, sizes, prev, pager, next" :total="total">
      </el-pagination>
    </div>

    <!--    // 新增更新-->
    <el-dialog title="请填写信息" v-model="dialogFormVisible">
      <el-form :model="form" :rules="rules" label-width="auto">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" v-if="operation == 1">
          <el-input v-model="form.password" placeholder="请输入密码" show-password></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item prop="email" label="邮箱">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item prop="status" label="权限">
          <el-select v-model="form.status">
            <el-option label="管理员" value="1" v-if="user.status == 1"></el-option>
            <el-option label="普通用户" value="2"></el-option>
            <!-- <el-option label="普通用户" value="3"></el-option> -->
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue';
import { getUser, saveUser, delUser } from '@/utils/category';
import { ElMessage } from 'element-plus';

const searchForm = ref({
  username: '',
  phone: ''
})
const user = ref(localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {})
const total = ref(0)
const currentPage = ref(1)
const currentSize = ref(10)
const tableData = ref([])
const dialogFormVisible = ref(false)
const operation = ref(1)
const form = ref({
  username: '',
  password: '',
  name: '',
  email: '',
  status: ''
})
const formR = ref({
  username: '',
  password: '',
  name: '',
  email: '',
  status: ''
})

onMounted(() => {
  search()
})

const findBySearch = () => {
  search()
}

const add = () => {
  dialogFormVisible.value = true
  form.value = formR.value
  operation.value = 1
}

const submitForm = () => {
  saveUser(form.value).then(res => {
    if (res.code == 200) {
      dialogFormVisible.value = false
      ElMessage.success('成功')
      search()
    } else {
      ElMessage.error('失败')
    }
  })
}

const search = () => {
  getUser(searchForm.value, currentPage.value, currentSize.value).then(res => {
    tableData.value = res.data.records
  })
}

const edit = (row) => {
  operation.value = 2
  dialogFormVisible.value = true
  form.value = JSON.parse(JSON.stringify(row))
}

const del = (row) => {
  delUser(row.id).then(res => {
    if (res.code == 200) {
      ElMessage.success('删除成功')
      search()
    } else {
      ElMessage.error('删除失败')
      search()
    }
  })
}
</script>
<style scoped lang="scss">
.dialog-footer {
  display: flex;
  justify-content: flex-end;
}

.el-pagination {
  float: right;
}
</style>